package sitemap;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GenerateSitemap {
    public static final String BASE_URL = "http://extramobi.ru";
    private static final String LASTMOD = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    private static final String CHANGE_FREQ = "weekly";
    private static final double PRIORITY = 0.8;

    public static int count = 0;

    public static void main(String[] args) throws IOException, InterruptedException {
        PrintStream out = new PrintStream(new FileOutputStream("sitemap.xml"));
        StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
        Document doc = Jsoup.connect(BASE_URL).timeout(60000).get();
        appendURL(sb, BASE_URL + "/pricelist/");
        // select all info pages
        Elements infoPages = doc.select("div.cpt_auxpages_navigation a");
        for (Element infoPage : infoPages) {
            count++;
            String pageUrl = BASE_URL + infoPage.attr("href");
            appendURL(sb, pageUrl);
        }
        // select all custom pages
        Elements customPages = doc.select("div.cpt_custom_html ul.vertical a");
        for (Element customPage : customPages) {
            count++;
            String pageUrl = BASE_URL + customPage.attr("href");
            appendURL(sb, pageUrl);
        }

        // select all 1st level categories
        Elements catList = doc.select("div.cpt_category_tree li a");
        for (Element cat : catList) {
            count++;
            String catUrl = BASE_URL + cat.attr("href");
            doc = Jsoup.connect(catUrl).timeout(60000).get();
            // select all second level categories
            Elements childCatList = doc.select("div.cpt_category_tree li.child a");
            appendURL(sb, catUrl);
            for (Element childCat : childCatList) {
                count++;
                catUrl = BASE_URL + childCat.attr("href");
                appendURL(sb, catUrl);
            }
            Elements offsets = doc.select("div.cpt_maincontent center p a.no_underline");
            int maxOffset = 0;
            if (offsets.size() <= 0) {
                // maxOffset = 0;
            } else if (offsets.get(offsets.size() - 1).text().equals("показать все")) {
                maxOffset = Integer.parseInt(offsets.get(offsets.size() - 3).text()) * 10;
            } else {
                maxOffset = Integer.parseInt(offsets.get(offsets.size() - 2).text()) * 10;
            }
            while (maxOffset > 0) {
                maxOffset -= 10;
                catUrl = BASE_URL + cat.attr("href") + "/offset" + maxOffset;
                doc = Jsoup.connect(catUrl).timeout(60000).get();
                // select all products
                Elements products = doc.select("center table div.prdbrief_name a");
                for (Element product : products) {
                    count++;
                    String productUrl = BASE_URL + product.attr("href");
                    appendURL(sb, productUrl);
                }
                System.out.println(count);
                Thread.sleep(100);
            }
        }
        sb.append("\n</urlset>");
        System.out.println(count);
        out.print(sb);
//        System.out.println(sb);
    }

    private static void appendURL(StringBuilder sb, String url) {
        sb.append("\n<url>\n\t<loc>").append(url).append("</loc>\n\t<lastmod>").
                append(LASTMOD).append("</lastmod>\n\t<changefreq>").
                append(CHANGE_FREQ).append("</changefreq>\n\t<priority>").
                append(PRIORITY).append("</priority>\n</url>");
    }

}
